<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录页面</title>
  <style>
    .logo {
      text-align: center;
      margin: 20px 0;
    }

    .container {
      width: 400px;
      margin: 50px auto;
      padding: 20px;
      border: 1px solid #ddd;
      border-radius: 5px;
    }

    .form-group {
      margin-bottom: 15px;
    }

    .form-group label {
      display: block;
      margin-bottom: 5px;
    }

    .form-group input {
      width: 100%;
      padding: 8px;
      border: 1px solid #ddd;
      border-radius: 4px;
    }

    .form-group select {
      width: 100%;
      padding: 8px;
      border: 1px solid #ddd;
      border-radius: 4px;
    }

    .btn {
      width: 100%;
      padding: 10px;
      background-color: #333;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }

    .register-link {
      text-align: center;
      margin-top: 15px;
    }
  </style>
</head>

<body>
  <div class="logo">
    <img src="/front_code/assets/out.gif" alt="网站Logo">
  </div>

  <div class="container">
    <form id="loginForm" onsubmit="return handleLogin(event)">
      <div class="form-group">
        <label>账号</label>
        <input type="text" id="account" placeholder="请输入账号" required>
      </div>
      <div class="form-group">
        <label>密码</label>
        <input type="password" id="password" placeholder="请输入密码" required>
      </div>
      <div class="form-group">
        <label>登录类型</label>
        <select id="userType" required>
          <option value="">请选择登录类型</option>
          <option value="patient">患者</option>
          <option value="doctor">医生</option>
          <option value="admin">管理员</option>
        </select>
      </div>
      <button type="submit" class="btn">登录</button>
    </form>
    <div class="register-link">
      还没有账号？<a href="注册页面.html">点击注册</a>
    </div>
  </div>

  <script>
    // 定义登录函数，提交表单后进行账号验证
    async function handleLogin(event) {
      event.preventDefault(); // 阻止表单的默认提交行为

      const account = document.getElementById("account").value;
      const password = document.getElementById("password").value;
      const userType = document.getElementById("userType").value;

      if (!userType) {
        alert("请选择登录类型！");
        return;
      }

      // 根据不同的用户类型，选择不同的后端接口进行验证
      let loginUrl = '';
      let loginData = {
        account: account,
        password: password
      };

      if (userType === 'patient') {
        loginUrl = 'http://localhost:8080/admin/login';  // 患者登录的后端接口
      } else if (userType === 'doctor') {
        loginUrl = 'http://localhost:8080/doctor/login';  // 医生登录的后端接口
      } else if (userType === 'admin') {
        loginUrl = 'http://localhost:8080/patient/login';  // 管理员登录的后端接口
      }

      try {
        // 使用 Fetch API 发送 POST 请求
        const response = await fetch(loginUrl, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify(loginData)
        });

        // 处理返回结果
        const result = await response.json();

        if (response.ok) {
          // 登录成功后，跳转到对应的页面
          if (userType === 'patient') {
            window.location.href = '患者端.html';
          } else if (userType === 'doctor') {
            window.location.href = '医生端.html';
          } else if (userType === 'admin') {
            window.location.href = '管理端.html';
          }
        } else {
          // 登录失败
          alert(result.message || '登录失败，请检查账号或密码');
        }
      } catch (error) {
        console.error('登录请求失败:', error);
        alert('登录请求失败，请稍后再试');
      }
    }
  </script>
</body>

</html>